package org.apache.webapp.balancer.rules;

import javax.servlet.http.HttpServletRequest;
import common.logging.WebPortalLogger;

/**
 * This class is based on the rule that the incoming web request
 * should go to the next server instance in the cluster..
 *
 * @author Srini Penchikala
 */
public class RoundRobinRule extends BaseLoadBalancingRule {

   public boolean matches(HttpServletRequest request) {
      //Log message
      String tomcatInstance="TC-LB";
      String appName="balancer";
      String objectName="RoundRobinRule";
      long t1=0;
      long t2=0;

      t1 = System.currentTimeMillis();
      common.logging.WebPortalLogger.logStartTime(tomcatInstance,appName,objectName,t1);
      //

      WebPortalLogger.info("getServerInstance() : " + getServerInstance());
      WebPortalLogger.info("getMaxServerInstances() : " + getMaxServerInstances());
      WebPortalLogger.info("getClusterMember()  : " + getClusterMember());

      int instance = new Integer(getServerInstance()).intValue();
      int maxInstances = new Integer(getMaxServerInstances()).intValue();
      int nextAvailableMember = 1;
      String host = getTcpListenAddress();
      int port = Integer.parseInt(getTcpListenPort());
      String webPage = getTestWebPage();
      boolean result = false;

      if (getClusterMember() == instance) {
         WebPortalLogger.info("Requested server instance match..");
         if (getClusterMember() == maxInstances) {
            nextAvailableMember = 1;
         } else {
            nextAvailableMember = getClusterMember() + 1;
         }
         setClusterMember(nextAvailableMember);
         // Check if the next member in the cluster is alive
//         if (isServerAlive(webPage)) {
         if (isServerAlive(host,port)) {
            WebPortalLogger.info("Cluster Member is alive.");
//            return true;
            result = true;
         } else {
            WebPortalLogger.info("Cluster Member is NOT alive.");
//            return false;
            result = false;
         }
      } else {
         WebPortalLogger.info("Requested server instance did not match..");
//         return false;
         result = false;
      }
      //
      t2 = System.currentTimeMillis();
      common.logging.WebPortalLogger.logFinishTime(tomcatInstance,appName,objectName,t1,t2);
      //

      return result;
   }

	/**
	 * Returns a String representation of this object.
	 *
	 * @return String
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer();

		buffer.append("[");
		buffer.append(getClass().getName());
		buffer.append(": ");

		buffer.append("RoundRobin - serverInstance: ");
		buffer.append(getServerInstance());

		buffer.append("; getClusterMember(): ");
		buffer.append(getClusterMember());

		buffer.append("; Redirect URL: ");
		buffer.append(getRedirectUrl());

		buffer.append("]");

		return buffer.toString();
	}
}
// End of RoundRobinRule.java
